package com.tth.learn.addproperties.java;

import org.junit.Test;

/**
 * 反射
 * @Author tth
 * @Date 2022/12/29 9:38
 */
public class ReflectTest {

    /**
     * 获取Class的实例的方式
     */
    @Test
    public void test() throws ClassNotFoundException {
        // 方式1：通过运行时类的属性：类.class
        Class<Person> clazz1 = Person.class;
        System.out.println(clazz1);

        // 方式2：通过运行时类的对象，调用getClass()
        Person p1 = new Person("",1);
        Class clazz2 = p1.getClass();
        System.out.println(clazz2);

        // 方式3：调用Class的静态方法：forName(String className)。其中className是类的完整的“包类”名
        Class<?> clazz3 = Class.forName("com.tth.learn.addproperties.java.Person");
        System.out.println(clazz3);

        // 方式4：使用类的加载器：ClassLoader
        ClassLoader classLoader = ReflectTest.class.getClassLoader();
        Class<?> clazz4 = classLoader.loadClass("com.tth.learn.addproperties.java.Person");
        System.out.println(clazz4);

        // 验证在一定时间内，获取的同一个类的运行时类，是同一个Class实例
        System.out.println(clazz1 == clazz2); // true
        System.out.println(clazz1 == clazz3); // true
        System.out.println(clazz1 == clazz4); // true
    }

    @Test
    public void test2() {
        System.out.println(int.class);
        System.out.println(void.class);
        System.out.println(Class.class);


        int[] arr1 = new int[10];
        int[] arr2 = new int[100];
        System.out.println(arr1.getClass() == arr2.getClass());
    }

    @Test
    public void test3() throws IllegalAccessException, InstantiationException {
        Class<Demo> clazz = Demo.class;
        Class<Person> clazz1 = Person.class;
        Person person = clazz1.newInstance();
    }

}

class A {
    static {
        m = 300;
    }

    static int m = 100;
}
